library(htmltools)

APP_URL <- "https://pchen-aitutor.hf.space"  # ← your Gradio app URL
USER_ID <- "anonymous_user"

browsable(tagList(
  # --- styles (fab, bubble, modal) ---
  tags$style(HTML("
    #tutor-fab{position:fixed;right:20px;bottom:20px;z-index:99999;padding:12px 14px;border-radius:999px;border:1px solid #ddd;background:#fff;box-shadow:0 8px 24px rgba(0,0,0,.15);cursor:pointer;font-weight:600}
    #tutor-fab:hover{background:#f6f8ff}
    #tutor-sel-btn{position:absolute;display:none;z-index:100000;padding:6px 10px;font-size:12px;border:1px solid #e5e7eb;border-radius:10px;background:#111827;color:#f9fafb;box-shadow:0 8px 24px rgba(0,0,0,.2);cursor:pointer;user-select:none}
    #tutor-modal-overlay{display:none;position:fixed;z-index:99998;inset:0;background:rgba(0,0,0,.35)}
    #tutor-modal{position:absolute;top:5%;left:50%;transform:translateX(-50%);width:min(980px,95vw);height:min(720px,90vh);background:#fff;border-radius:10px;box-shadow:0 10px 30px rgba(0,0,0,.25);overflow:hidden}
    #tutor-modal-header{display:flex;justify-content:space-between;align-items:center;padding:10px 14px;background:#f5f5f7;border-bottom:1px solid #e5e5ea;font-weight:600}
    #tutor-close-btn{cursor:pointer;border:none;background:transparent;font-size:18px}
    #tutor-iframe{width:100%;height:calc(100% - 18px);border:0}
  ")),

  # modal + iframe
  tags$div(id="tutor-modal-overlay",
    tags$div(id="tutor-modal",
      tags$div(id="tutor-modal-header","AI Calculus Tutor", tags$button(id="tutor-close-btn","✕")),
      tags$iframe(id="tutor-iframe", src="")
    )
  ),

  # floating button + selection bubble
  tags$button(id="tutor-fab","Ask Tutor"),
  tags$button(id="tutor-sel-btn","Ask Tutor"),

  # script
  tags$script(HTML(sprintf("
    (function(){
      const APP_URL = '%s';
      const USER_ID = '%s';

      function sel(){ try { return (window.getSelection?window.getSelection().toString():'').trim(); } catch(e){ return ''; } }
      function openInline(q, action){
        const p = new URLSearchParams({ user_id: USER_ID, q: q||'', action: action||'prefill' });
        const frame = document.getElementById('tutor-iframe');
        frame.src = APP_URL + '/?' + p.toString();          // Prefill handled by app from URL
        document.getElementById('tutor-modal-overlay').style.display='block';
      }
      function openTab(q, action){
        const p = new URLSearchParams({ user_id: USER_ID, q: q||'', action: action||'prefill' });
        window.open(APP_URL + '/?' + p.toString(), 'ai_tutor_tab', 'noopener');
      }

      // floating button: prefill by default; Alt=send; Ctrl/Cmd=new tab
      document.getElementById('tutor-fab').addEventListener('click', function(ev){
        const q = sel();
        const act = ev.altKey ? 'send' : 'prefill';
        if (ev.ctrlKey || ev.metaKey) openTab(q, act); else openInline(q, act);
      });

      // selection bubble near highlighted text
      const b = document.getElementById('tutor-sel-btn');
      let last='';
      function place(x,y){ b.style.left=(x+10)+'px'; b.style.top=(y+10)+'px'; }
      function show(){ b.style.display='block'; } function hide(){ b.style.display='none'; }
      document.addEventListener('mouseup', e => setTimeout(() => {
        const t = sel(); if (t && t.length>=2 && t.length<=800){ last=t; place(e.pageX,e.pageY); show(); } else hide();
      }, 20));
      window.addEventListener('scroll', hide, {passive:true}); window.addEventListener('resize', hide);

      // bubble click: prefill by default; Alt=send; Ctrl/Cmd=new tab
      b.addEventListener('click', function(ev){
        if (!last) return;
        const act = ev.altKey ? 'send' : 'prefill';
        if (ev.ctrlKey || ev.metaKey) openTab(last, act); else openInline(last, act);
        hide();
      });

      // close modal
      const overlay = document.getElementById('tutor-modal-overlay');
      document.getElementById('tutor-close-btn').addEventListener('click', () => overlay.style.display='none');
      overlay.addEventListener('click', e => { if (e.target===overlay) overlay.style.display='none'; });
    })();
  ", APP_URL, USER_ID)))
))
AI Calculus Tutor

Learning Objectives

By the end of this 90-minute session, students will be able to:

  1. Define and plot univariable functions in R for economic applications
  2. Define and plot multivariable functions in R for business optimization
  3. Solve linear equation systems using matrix notation and operations
  4. Analyze quadratic functions and positive definite matrices with their graphical representations

R skills

  1. Define a function using the R key word function()
  2. Define a function using a function as input
  3. 3D visualisation of multivariable functions

Knowledge Point 1: Defining and Plotting Univariable Functions in R

Example 2.1: The Coffee Shop Economics

Bean & Brew Café wants to optimize its operations using mathematical modeling. The owner needs to understand:

  1. Production function: \(Q(L) = 10\sqrt{L}\) (cups per hour vs labor hours)
  2. Cost function: \(C(Q) = 50 + 2Q + 0.1Q^2\) (total cost vs output)
  3. Revenue function: \(R(Q) = 5Q - 0.05Q^2\) (revenue vs quantity sold)
  4. Profit function: \(\Pi(Q) = R(Q) - C(Q)\) (profit optimization)

The owner asks: “How can I use R to visualize these relationships and find the optimal production level?”

In R, functions can be defined using several approaches:

Method 1: Vectorised operations in R

By vectorised operation we mean the variable \(x\) is a sequence of real numbers as we did in the previous section. In mathematics we call such \(x\) a vector. Vectorised operation creates a new sequence \(y\). It matches to each number in the sequence of \(x\) a number in the new sequence of \(y\).

R code for a function defined by vectorised operation is like follows, which we have used in the previous section.

x <- seq(from, to, by = step)
y <- expression_in_x

Method 2: Using function() keyword

R code using function() is like follows:

f <- function(x) {
  return(expression_in_x)
}

For \[y = 0.5x + 2\]

The following R code defines the function and plot its graph.

f <- function(x) {
  return(0.5*x+2)
}
x <- seq(-4,8,1)
y <- f(x)

plot(x, y, type = "l", main = "Linear Function Y = 0.5 X + 2", xlab = "X", ylab = "Y")

Often the vectorized operation method is simple but it is less flexible and in some situation it is not applicable.

Example 1.2 The delivery service (revisited)

The price is a piece-wise linear function.

\[C(d) = \begin{cases} 10 & \text{if } 0 \leq d \leq 5 \\ 10 + 2(d-5) & \text{if } 5 < d \leq 15 \\ 10 + 20 + 1.5(d-15) & \text{if } d > 15 \end{cases}\]

In this case we can only use function() to define the price function in R.

C<-function(d){
  if ((d>0)&( d <= 5))  {price<-10}
  if ((d>5)&( d <= 15)) {price<- 10 + 2*(d-5)}
  if (d>15)             {price <- 10 + 20 + 1.5*(d-15)}
  return(price)
}

d<- seq(1,30)
P<-d*0
# here we cannot do P <- D(d) because the condition in function C(d) refers d as a single number not sequence. 
# hence we match each component of P to the corresponding component of d one by one
for (i in 1:length(d)) {
  P[i] = C(d[i])
}

plot(d,P,type = "l", main = "Price of Delevery", 
     xlab = "Distance in KM", ylab = "Price")

Exercise

Use R keyword function() to define the following function and draw its graph over the domain of [0,10] in following R Environment or in your own local RStudio. You can copy the sample code above and modify it to define the function.

\[f(x) = \begin{cases} 2x & \text{if } 0 \leq x \leq 5 \\ 10 + 4(x-5) & \text{if } 5 < x \\ \end{cases}\]

Plot a function using the function as input

R function() allows more types of input than real numbers. We take function as input to generate a desired output. In this sense function() extends the concept of real functions. Here is an example of how to define a Plot function that takes a function and a vector variable as input, and gives the graph of the function over the domain given by the vector as output.

Plot_function <- function(f,x) {
  y <- f(x)
  plot(x,y,type="l")
}

### note: Plot_function can be used to plot any function of $x$. See following two  examples

## we define a function Line_point_slope: this function goes through the point (x0,y0) with a slope equals b
x0 = 3
y0 = 4
b  = 2
Line_point_slope <- function(x){
  y = y0 + b*(x-x0)
  return(y)
}

## we define a function Polynomial3: this polynomial is of order 3 

Polynomial3 <- function(x){
  y = x^3+2*x^2-4*x-1
  return(y)
}


x <- seq(0,10,0.1)
Plot_function(Line_point_slope,x)

Plot_function(Polynomial3,x)

Exercise

Use R keyword function() to define a function that use a yet to be specified function \(f\) as input and draw the graphs of \(f\) over a subset of the domain of \(f\) in the following R Environment or in your own local RStudio. Then specify different function \(f\) and draw the graph of \(f\).

  • A linear function that goes through a point \((x_0,y_0)= (2,3)\) and has the slope \(b=1.5\).

  • \(f(x) = \sin(2x)\),

  • \(f(x)=\frac{1}{1+e^-x}\),

  • \(f(x) =\frac{x^3+1}{5+x^2}\))

The R code of the two graphs above can be used as a template to complete this exercise.


Knowledge Point 2: Define and Plot Multivariable Functions in R

Example 2.2: The Tech Company’s Strategic Planning

InnovateTech Corp is optimizing its operations across multiple dimensions:

  1. Utility Function: \(U(x,y) = x^{0.6}y^{0.4}\) (consumer satisfaction from products x and y)
  2. Profit Function: \(\Pi(p_1,p_2) = (p_1-10)q_1 + (p_2-15)q_2\) where \(q_i = 100-2p_i\) (profit from two products)
  3. Production Function: \(Q(L,K) = 20L^{0.7}K^{0.3}\) (output from labor L and capital K)

The CEO asks: “How can we visualize these multivariable relationships in R to make better strategic decisions?”

Definition 2.1: Multivariable Function

A function \[f : \mathbb{R}^n \to \mathbb{R}\] is called a multivariable function if it maps multiple inputs \((x_1,x_2,...,x_n)\) to a real number \(f(x_1,x_2,...,x_n)\).

Example of a linear function \[f(x,y) = x + 2y - 4\] Thsi function takes two inputs \(x\) and \(y\) and return a value that depends lienarly on \(x\) and \(y\).

Example of a quadratic function \[f(x,y) =x^2+y^2\] This function takes two inputs \(x\) and \(y\) and returns their squared sum. This is a function of two variables.

Multivariable Function in R

f<-function(x,y){
  return(x^2+y^2)
}

In plotting a univariable function \(y = f(x)\),

    1. we create a sequence of values for the input variable \(x\)
    1. we calculate the values of the output variable \(y\) corresponding to each input variable value.
    1. Then we plot these pairs of \((x,y)\) in the coordinate system and connect them with lines.

In plotting a multivariable function \(z=f(x,y)\) we follow the same procedure.

    1. we create a sequence of values for each input variable \(x\) and \(y\) respectively,
    1. we calculate the value of the output variable \(z\) corresponding to each pair of input values.
    1. Then we plot the triple \((x,y,z)\) in the 3D space of the coordinate system and connect them with lines (surfaces).

Visualise linear functions

\[f(x,y) = x+ 2y -4\] A function of two variables can be visualised as a surface over in a 3D coordinate system, where \((x,y)\) are the input variables and \(z\) is the value of the output variable. For the linear function the surface will be a plane.

As in the case of univariate functions, we create sequences for the input variables \(x\) and \(y\). For each pair \((x,y)\) we calculate the function value \(z=f(x,y)\). The resulting \(z\)-values are then connected to form the surface.

Surface plot

x <- seq(-10, 10, length.out = 30)
y <- seq(-10, 10, length.out = 30)

f <- function(x, y) { Q <- x + 2*y - 4 }
z <- outer(x, y, f)

#op <- par(bg = "white")
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")

Contour Plot

One often used visualisation of a multivariable function is the contour plot. The following link shows a contour plot of Snowing Mountain in googlemap.

https://www.google.com/maps/place/Snowy+Mountains/@-36.5044076,148.3164928,14.6z/data=!4m6!3m5!1s0x6b234b038cf2dd5d:0x5e23945ff8d761b8!8m2!3d-36.5!4d148.3333333!16zL20vMDc0d20!5m2!1e4!1e3?entry=ttu&g_ep=EgoyMDI1MDgyNS4wIKXMDSoASAFQAw%3D%3D

Rice fields on hills create contour lines (level lines) in the landscape. Since rice needs water, the ridges around the fields must be kept even.

Rise field and Isoquants
Rise field and Isoquants

The lines of seats in a stadium are similar to the contour lines of a plane.

Along the contour line the height is a constant, therefore contour lines are also called level lines. They are also known as isoquants. Perpendicular to the contour lines is the direction of the steepest descending or ascending.

contour(x, y, z, nlevels = 20)

Station stairs and Isoquants
Station stairs and Isoquants

Heat map

Heat map
Heat map

Heat map is often used in weather forecast to visualise the the temperature function over an area. It is a function of two variables the longitude and the latitude. The color corresponds to the function value, the temperature.

For the linear function in the exampl:

filled.contour(x, y, z)

Gradient fieled

While the heat map with contour lines show the along which direction the surface is more flat, it shows at same time that the direction perpendicular to the contour lines is the direction of ascending of descending. Gradient field is a plot that shows which direct is the most ascending direction at each point with an arrow.

plot(1, type = "n", xlim = range(x), ylim = range(y),
     xlab = "Labor (L)", ylab = "Capital (K)",
     main = "Gradient Field of Q(L,K)")


# Define partial derivatives (gradient components)
dQ_dx <- function(x, y) {
  1  # ∂Q/∂L
}

dQ_dy <- function(x, y) {
  2  # ∂Q/∂K
}


for(l in x) {
  for(k in y) {
    arrows(l, k, 
           l + dQ_dx(l, k)/8, 
           k + dQ_dy(l, k)/8,
           length = 0.04, col = "blue")
  }
}

On a plane, the direction of maximum ascent is the same everywhere; therefore, the arrows are identical at each point.

Interactive surface

Interactive surface plot allows to see the 3D object from different perspective and make it a “real” 3D plot.

library(plotly)
plot_ly(x = x, y = y, z = z, type = "surface")

Slice

Slice is a 2D plot generated by assigning other input variables some constant values. In this way the function value depends only on one input variable. This method is oftne used in ecomonics to investigate the impact on one paritcula variables on the multivariable function.

# --- Slice Plot ---
plot(x, f(x, y = 0), type = "l", col = "blue", lwd = 2,
     xlab = "Labour (L)", ylab = "Output", main = "Slice of Production Function (y = 0,2,4)")
lines(x, f(x, y = 2), col = "red", lwd = 2)
lines(x, f(x, y = 4), col = "green", lwd = 2)
legend("bottomright", legend = c("K = 0", "K = 2","K = 4"), col = c("blue", "red","green"), lwd = 2)

Visualisation of Nonlinear Functions

\[Q(L,K)=20x^{0.7}y^{0.3}\]

Surface plot

L <- seq(0, 10, length.out = 30)
K <- seq(0, 10, length.out = 30)

f <- function(L, K) { Q <- 20*L^0.7*K^0.3 }
z <- outer(L, K, f)

#op <- par(bg = "white")
persp(L, K, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")

Contour Plot

contour(L, K, z, nlevels = 20)

Heat Map

filled.contour(L, K, z)

Gradient Field

# Define partial derivatives (gradient components)
dQ_dL <- function(L, K) {
  20 * 0.7 * L^(-0.3) * K^0.3  # ∂Q/∂L
}

dQ_dK <- function(L, K) {
  20 * 0.3 * L^0.7 * K^(-0.7)  # ∂Q/∂K
}


plot(1, type = "n", xlim = range(L), ylim = range(K),
     xlab = "Labor (L)", ylab = "Capital (K)",
     main = "Gradient Field of Q(L,K)")

for(l in L) {
  for(k in K) {
    arrows(l, k, 
           l + dQ_dL(l, k)/100, 
           k + dQ_dK(l, k)/100,
           length = 0.04, col = "blue")
  }
}

In a gradient field plot, the direction of an arrow points to the direction of ascent, and the length of the arrow indicates the steepness of that ascent. The longer the arrow, the steeper the ascent.

Interactive surface

library(plotly)
plot_ly(x = L, y = K, z = z, type = "surface")

Slice

# --- Slice Plot ---
plot(L, f(L, K = 5), type = "l", col = "blue", lwd = 2,
     xlab = "Labour (L)", ylab = "Output", main = "Slice of Production Function (K = 5)")
lines(L, f(L, K = 2), col = "red", lwd = 2)
lines(L, f(L, K = 1), col = "green", lwd = 2)
legend("bottomright", legend = c("K = 5", "K = 2","K = 1"), col = c("blue", "red","green"), lwd = 2)

Visualization Techniques

  1. Surface plots: Show the 3D shape of the function
  2. Contour plots: Show level curves (isoquants, indifference curves)
  3. Heat maps: Color-coded representation of function values
  4. Gradient field: Show the direction and steepness of ascent at each point
  5. Cross-sections: 2D slices of the 3D function

Economic Interpretations

  • Isoquants: Curves of constant output in production functions
  • Indifference curves: Curves of constant utility
  • Iso-profit curves: Curves of constant profit
  • Gradients: Direction of steepest increase

Exercise

Visualise the following functions using surface plot, contour plot, heat map, slice, and 3D interactive surface plot.

  • a plane that goes through \((x_0,y_0) = (2,3)\) with slope a=1.5 in x direct and slope b=3 in y direction. (Hint: the plane function is \(f(x,y) = f(x_0,y_0) + a(x-x_0) + b(y-y_0)\)

  • \(f(x,y) = x^2+y^2\)

using the following R Environment or your own local RStudio.

Interactive Quiz 2.2

Knowledge Point 3: Linear Equation Systems and Matrix Notation

Example 1.4: The Market Equilibrium (Revisited)

Finding where supply equals demand:

  • Supply Function: Q = -100 + 2P (suppliers willing to sell)
  • Demand Function: Q = 300 - P (consumers willing to buy)

This requires us to solve this system of two linear equations at the same time.

Example 2.3: The Manufacturing Company

TechProd Manufacturing produces three products: smartphones (S), tablets (T), and laptops (L). The company needs to determine optimal production levels based on resource constraints.

Resource Requirements per Unit:

  • Labor hours: 2S + 3T + 4L = 1000 hours available

  • Materials (kg): 1S + 2T + 3L = 600 kg available

  • Machine time: 3S + 1T + 2L = 800 hours available

This real-world problem naturally required us to solve this system of 3 linear equations at same time.

How do we want to proceed? Recall how we solve a single linear equation:

\[ax+b=0\] Procedure:

Step 1: Move the unknown to the right hand side and the known to the left hand side. \[ax = -b\] Step 2: Multiply the inverse of the coefficient \(a\) to both side of the equation.

\[a^{-1}ax = a^{-1}(-b)\] This leads the thesolution: \[x = -a^{-1}b\] For \(a=3\) and \(b=2\), R solution is as follows:

a = 3
b = 2

-solve(a)*b
##            [,1]
## [1,] -0.6666667

Can we follow the same procedure to solver a system of linear equations? The answer is yes. Here is how.

Example 2.4: The Manufacturing Company

step 1: Move all unknowns to the left and side of the equation and the knowns to the right hand side of the equation, and arrange the unknown variables in the same order: i.e. \(S\) first, \(T\) second, and \(L\) third

\[2S + 3T + 4L = 1000\] \[1S + 2T + 3L = 600\] \[3S + 1T + 2L = 800\] step 1.1

Put all unkonwns into one sequence as a vector \(X=\left( \begin{array}{c} S\\ T\\ L\\ \end{array} \right)\)

and the knowns on the right hand side as a vector \(b=\left( \begin{array}{c} 1000\\ 600\\ 800\\ \end{array} \right)\)

step 1.2

Put the coefficients of the equation system into a matrix;

\(A=\left( \begin{array}{ccc} 2&3&4\\ 1&2&3\\ 3&1&2\\ \end{array} \right)\)

Note the for a 3 equations system, we have 3 unknowns, i.e. \(X\) has 3 elements: \(X=\left( \begin{array}{c} S\\ T\\ L\\ \end{array} \right)\); the right hand side knowns \(b\) also has 3 elements: \(b=\left( \begin{array}{c} 1000\\ 600\\ 800\\ \end{array} \right)\);

the coefficient matrix is a \(3 \times 3\) square matrix, the values in the matrix correspond to their positions in the three equations. ( This is why the order of the variables in the equation is sensitive.)

\[\underbrace{\left( \begin{array}{rrr} 2 & 3 & 4\\ 1 & 2 & 3\\ 3&1 & 2\\ \end{array} \right)}_{A} \underbrace{\left( \begin{array}{c} S\\ T\\ L\\ \end{array} \right)}_{X} = \underbrace{\left( \begin{array}{r} 1000\\ 600\\ 800\\ \end{array} \right)}_{b} \]

Step 2 Multiply the inverse of the coefficient \(A\) to both side of the equation

\[A^{-1}AX = A^{-1}b\] This leads to the solution \(A^{-1}A=I\):

\[X = A^{-1}b\]

R solution is as follows:

A <- matrix(c(2,3,4,1,2,3,3,1,2),3,3,byrow = TRUE)
b <-c(1000,600,800)

solve(A)%*%b
##              [,1]
## [1,] 2.000000e+02
## [2,] 2.000000e+02
## [3,] 5.684342e-14
# %*% is the symbol of matrix multiplication in R.
# *   is the symbol of real number multiplication. 

The solution is \(S=200\), \(T=200\), and \(L=0\).

Example 1.4: The Market Equilibrium (Revisited)

  • Supply Function: Q = -100 + 2P
  • Demand Function: Q = 300 - P

After moving the unknowns to the left hand side, We have in matrix form:

\[\underbrace{\left( \begin{array}{rr} 1 & -2 \\ 1 & 1 \\ \end{array} \right)}_{A} \underbrace{\left( \begin{array}{c} Q\\ P\\ \end{array} \right)}_{X} = \underbrace{\left( \begin{array}{r} -100\\ 300\\ \end{array} \right)}_{b} \]

R solution

A = matrix(c(1,-2,1,1),2,2,byrow=TRUE)
b = c(-100,300)
solve(A)%*%b
##          [,1]
## [1,] 166.6667
## [2,] 133.3333
# %*% is the symbol of matrix multiplication in R.
# *   is the symbol of real number multiplication. 

This is exactly the same solution we have obtained by substitution method in the previous section.

Existence of one solution, infinitly mand solutions and no solution

  • One solution: Two line cross each other: there is one solution \(\Leftrightarrow\)\(A\) is invertible \(\Leftrightarrow\) the slopes are different.
  • Infinitely many solutions: There is only one line. \(A\) is not invertible\(\Leftrightarrow\) slopes and intercepts are the same \(\Leftrightarrow\) the rows including the constants are proportional.
  • No solution: Two lines are parallel to each other, \(A\) is not invertible \(\Leftrightarrow\) the slopes are the same but the intercepts are different \(\Leftrightarrow\) The rows of \(A\) are proportional but not the constants.
Existence of solutions
Existence of solutions

Exercise

Solve the market equilibrium problem

  • Supply Function: Q = -80 + 2P
  • Demand Function: Q = 200 - 2P

above using R Environment below or using your own local RStudio.

Formal Presentation of Linear Equation Systems

Definition 2.2: Linear Equation System

A system of \(m\) linear equations in \(n\) unknowns has the form: \[\begin{cases} a_{11}x_1 + a_{12}x_2 + \cdots + a_{1n}x_n = b_1 \\ a_{21}x_1 + a_{22}x_2 + \cdots + a_{2n}x_n = b_2 \\ \vdots \\ a_{m1}x_1 + a_{m2}x_2 + \cdots + a_{mn}x_n = b_m \end{cases}\]

Matrix Representation

This system can be written compactly as: \(\mathbf{Ax} = \mathbf{b}\)

Where: - \(\mathbf{A} = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix}\) ,\(\hspace{0.5cm}\) \(\mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix}\) ,\(\hspace{0.5cm}\) \(\mathbf{b} = \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_m \end{bmatrix}\)

Solution

\[\mathbf{x} = \mathbf{A}^{-1}\mathbf{b}\]

Matrix and Vector Operations and Geometric Interpretation

We have been using vector operations to define a function in R Since last section. The example of solving linear equation systems using matrix notation demonstrates the usefulness of vector and matrix notations. Now we want to formalise these operations to see what kind other benefits these vector and matrix notation can give.

    1. Vector Addition:\((\mathbf{x}+\mathbf{b})_i = x_i+b_i\)
    1. Vector Multiplication with a Number:\((a\mathbf{x})_i = ax_i\)
    • Scaling and potential direction reversal. \(\mathbf{x}\) and \(\mathbf{y}\) are parallel if \(\mathbf{x}=a\mathbf{y}\).
    1. Inner Product of Two Vectors:\(\mathbf{x}'\mathbf{y} = \sum_{k=1}^nx_ky_k\)
    • Measuring alignment/projection between vectors. \(\mathbf{x}'\mathbf{y}=0\), or \(\begin{bmatrix} x_1 & x_2 \end{bmatrix}\begin{bmatrix} y_1 \\ y_2 \end{bmatrix}=0\) implies the two vectors are perpendicular to each other.
    1. Outer Product of Two Vectors:\(\mathbf{x}\mathbf{y}'_{i,j} = x_iy_j\)
    1. Matrix Addition: \((\mathbf{A} + \mathbf{B})_{ij} = a_{ij} + b_{ij}\)
    1. Matrix Multiplication: \((\mathbf{AB})_{ij} = \sum_{k=1}^{p} a_{ik}b_{kj}\)
    1. Matrix Multiplication With a Vector: \((\mathbf{Ax})_{i} = \sum_{k=1}^{n} a_{ik}x_{k}\) -From the rule of matrix and vector multiplication we know a linear equation system \[\begin{cases} a_{11}x_1 + a_{12}x_2 + \cdots + a_{1n}x_n = b_1 \\ a_{21}x_1 + a_{22}x_2 + \cdots + a_{2n}x_n = b_2 \\ \vdots \\ a_{m1}x_1 + a_{m2}x_2 + \cdots + a_{mn}x_n = b_m \end{cases}\] can be written as: \[\mathbf{Ax} = \mathbf{b} \]

Properties: - Associative: \((\mathbf{AB})\mathbf{C} = \mathbf{A}(\mathbf{BC})\) - Distributive: \(\mathbf{A}(\mathbf{B} + \mathbf{C}) = \mathbf{AB} + \mathbf{AC}\) - Not commutative: \(\mathbf{AB} \neq \mathbf{BA}\) (in general)

Two important relations between two vectors

  • Two vectors are parallel to each oether: \(\mathbf{x} = \lambda \mathbf{y}\) or \[\begin{bmatrix} x_1 \\ x_2 \end{bmatrix}=\lambda \begin{bmatrix} y_1 \\ y_2 \end{bmatrix}\].

  • Two vectors are perpendicular to each other: \(\mathbf{x}'\mathbf{y} = 0\) or \[\begin{bmatrix} x_1 & x_2 \end{bmatrix} \begin{bmatrix} y_1 \\ y_2 \end{bmatrix}=0\].

Exercise

A vector with \(n\) elements is called an \(n\)-vector. An matrix with \(m\) rows and \(n\) columns is called an \(m\times n\)-matrix. Verify the following statement using the Matrix operation rules above.

  • Sum of two \(n\)-vectors is an \(n\)-vector.
  • An \(n\)-vector is an \(n\times 1\) matrix.
  • Inner product of two vectors is a number.
  • Outer product of an \(m\)-vector with an \(n\)-vector is an \(m\times n\) matrix.
  • Sum of two \(m\times n\) matrices is a \(m\times n\) matrix.
  • Product of an \(m\times n\) matrix and an \(n\times l\) matrix is an \(m\times l\) matrix.
  • Product of an \(m\times n\) matrix and an \(n\)-vector matrix is an \(m\)-vector.

Interactive Quiz 2.2


Inverse Matrix and Solving Linear Equation Systems

Formal Presentation

Definition 2.3: Matrix Inverse

For a square matrix \(\mathbf{A}\), the inverse matrix \(\mathbf{A}^{-1}\) satisfies: \[\mathbf{A}\mathbf{A}^{-1} = \mathbf{A}^{-1}\mathbf{A} = \mathbf{I}\]

where \(\mathbf{I}\) is the identity matrix.

Existence Conditions

\(\mathbf{A}^{-1}\) exists if and only if: 1. \(\mathbf{A}\) is square (\(n \times n\)) 2. \(\det(\mathbf{A}) \neq 0\) (A is non-singular)

Interpretation

  • Determinant ≠ 0: The system has a unique solution (feasible allocation)
  • Determinant = 0: Either no solution (impossible constraints) or infinite solutions (redundant constraints)

Geometric explanation: determinant=0 implies lines/planes are parallel.

  • Parallel line with different intercepts, there will be no intersection \(\to\) no solution.
  • Parallel line with same intercepts, two lines overlaps each other \(\to\) infinite solutions.

Computing the Inverse

Using pencil and paper for 2×2 matrices: \[\mathbf{A}^{-1} = \frac{1}{\det(\mathbf{A})} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}\] where \(\mathbf{A} = \begin{bmatrix} a & b \\ c & d \end{bmatrix}\) and \(\det(\mathbf{A}) = ad - bc\)

For \(\mathbf{A} = \begin{bmatrix} 2 & 3 \\ 3 & 5 \end{bmatrix}\), we have following the formula: \(\mathbf{A}^{-1} = \begin{bmatrix} 5 & -3 \\ -3 & 2 \end{bmatrix}\),

Using R

### assign the matrix
A = matrix(c(2,3,3,5),2,2,byrow=TRUE)
### calculate the determinant
det(A)
## [1] 1
solve(A)
##      [,1] [,2]
## [1,]    5   -3
## [2,]   -3    2

Computation of an inverse matrix is doable with pencil and paper only for very small matrixes such as \(2\times 2\) matrices. For large matrices it is always done with computer.

Exercise

Calculate the determinant and the inverse of \(\mathbf{A} = \begin{bmatrix} 2 & 3 \\ 3 & 5 \end{bmatrix}\) using the following R Environment or your local RStudio.

Geometric illustration of existence of solution in linear equation system

  • One solution: three plane intersection at one point: there is a unique solution \(\Leftrightarrow\)\(A\) is invertible \(\Leftrightarrow\) the slopes of the planes are different.
  • Infinitely many solutions: one plane is redudant. \(A\) is not invertible\(\Leftrightarrow\) slopes and intercepts are the same for at least two planes \(\Leftrightarrow\) the rows including the constants are proportional.
  • No solution: Two planes are parallel to each other, \(A\) is not invertible \(\Leftrightarrow\) the slopes are the same but the intercepts are different for at least two planes\(\Leftrightarrow\) At least two rows of \(A\) are proportional but not the constants.

Interactive Quiz 2.3

Solving a nonlinear equation system

Solving a nonlinear equation system is a much more complicated task than solving a linear equation system. Following the geometric interpretation, one nonlinear equation with two variables is a curve on the (x,y) plane. A two equations system of two variables consists of two curves. If the two curves intersect all intersection points are solutions. We can use the following interactive graph to explore the solutions of a two nonlinear equations system of two variables.


Knowledge Point 4: Quadratic Functions and Positive Definite Matrices

In business and economics the following function is of particular interest \[f(x) = ax^2\]

Question: When is this function always positive (except at \(x=0\))? (e.g. the business activity will grantee a positive result)

Answer: When \(a > 0\)

This is the one variable case of positive definiteness: a function that always curves upwards and has a minimum at 0.

Extending this to multivariate cases, now consider \[f(\bf{x}) = \bf{x}'A \bf{x}= \sum_{i=1}^n\sum_{j=1}^n a_{i,j}x_ix_j\] where \(\bf{x}\) is a vector and \(A\) is a square \(n\times n\) matrix. This is the multivariate version of the scalar quadratic function. If this function is always positive except \(\bf{x=0\), then \(A\) is called positive definite.

Example 2.5: Hooke’s law and spring energy

In physics, the potential energy stored in a stretched spring is always positive (unless there’s no displacement):

\[ E = \frac{1}{2}\bf{x}'K\bf{x}\] where \(K\) is a stiffness matrix. For the system to make physical sense, energy must be positive \(\to\) \(K\) must be positive definite.

Example 2.6: The Investment Risk Analysis

Global Portfolio Management is analyzing investment risk using quadratic forms. The firm’s risk model for a two-asset portfolio is:

\[\text{Risk} = w_1^2\sigma_1^2 + w_2^2\sigma_2^2 + 2w_1w_2\sigma_{12}\]

Where: + \(w_1, w_2\) are portfolio weights

  • \(\sigma_1^2, \sigma_2^2\) are asset variances

  • \(\sigma_{12}\) is the covariance between assets

This can be written as a quadratic form: \(\text{Risk} = \mathbf{w}^T\mathbf{\Sigma}\mathbf{w}\) with \(\mathbf{w}=\left( \begin{array}{c} w_1\\ w_2\\ \end{array} \right)\) and \(\mathbf{\Sigma} =\left(\begin{array}{cc} \sigma_1^2& \sigma_{12}\\ \sigma_{12}& \sigma_2^2\\ \end{array} \right)\)

The risk manager asks: “How can we determine if our covariance matrix ensures the risk is always positive (positive definite), and how do we visualize this?”

Definition 2.4: Positive Definite Matrices

A symmetric matrix \(\mathbf{A}\) is positive definite if: \[\mathbf{x}^T\mathbf{A}\mathbf{x} > 0 \text{ for all } \mathbf{x} \neq \mathbf{0}\]

Equivalent conditions:

  1. All eigenvalues of \(\mathbf{A}\) are positive

  2. All leading principal minors are positive

  3. \(\mathbf{A} = \mathbf{L}\mathbf{L}^T\) for some invertible matrix \(\mathbf{L}\) (Cholesky decomposition)

Calculation of eigen value in R

A<- matrix(c(2,1,1,3),2,2,byrow=TRUE)
A
##      [,1] [,2]
## [1,]    2    1
## [2,]    1    3
eigen(A)
## eigen() decomposition
## $values
## [1] 3.618034 1.381966
## 
## $vectors
##           [,1]       [,2]
## [1,] 0.5257311 -0.8506508
## [2,] 0.8506508  0.5257311

Graphical Properties

  • Positive definite:
    • Elliptical contours, unique minimum
    • Surface is bending upwards
# Create a grid over [-10, 10]
x <- seq(-10, 10, length.out = 100)
y <- seq(-10, 10, length.out = 100)

z <- outer(x, y, function(x, y) {
  mapply(function(x, y) {
    X <- c(x, y)
    t(X) %*% A %*% X
  }, x, y)
})

filled.contour(x, y, z)

Interactive surface

# --- Interactive 3D Surface ---
library(plotly)
plot_ly(x = x, y = y, z = z, type = "surface")

Negative Definite Matrices

A symmetric matrix \(\mathbf{A}\) is negative definite if: \[\mathbf{x}^T\mathbf{A}\mathbf{x} < 0 \text{ for all } \mathbf{x} \neq \mathbf{0}\]

All eigenvalues of \(\mathbf{A}\) are negative

Calculation of eigen value in R

A<- matrix(c(-2,1,1,-3),2,2,byrow=TRUE)
A
##      [,1] [,2]
## [1,]   -2    1
## [2,]    1   -3
eigen(A)
## eigen() decomposition
## $values
## [1] -1.381966 -3.618034
## 
## $vectors
##            [,1]       [,2]
## [1,] -0.8506508 -0.5257311
## [2,] -0.5257311  0.8506508

Graphical Properties

  • Negative definite:
    • Elliptical contours, unique maximum
    • the surface is beding downwards
# Create a grid over [-10, 10]
x <- seq(-10, 10, length.out = 100)
y <- seq(-10, 10, length.out = 100)

z <- outer(x, y, function(x, y) {
  mapply(function(x, y) {
    X <- c(x, y)
    t(X) %*% A %*% X
  }, x, y)
})


filled.contour(x, y, z)

Interactive surface

# --- Interactive 3D Surface ---
library(plotly)
plot_ly(x = x, y = y, z = z, type = "surface")

Exercise

For \(A =\begin{bmatrix} -4 & -1 \\ -1 & -6 \end{bmatrix}\) create 3D plots of the quadratic function \(f(\bf{x}) =\bf{x}'A\bf{x}\) over the domain of \(x_1 \in [-1,1]\),\(x_2\in [-1,1]\) using the following R environment of using your own local Rstudio.

Interactive Quiz 2.4


Comprehensive Review Questions

This section provides review and application questions designed to assess understanding at five different levels. Each level builds upon the previous one, fostering deeper comprehension and the ability to apply concepts in increasingly complex situations.

https://pchen-exercisesfeedbackselect.hf.space


Summary and Next Steps

This comprehensive Section 2 covers the essential mathematical foundations for business analysis:

  1. Linear systems and matrices - The backbone of resource allocation and constraint optimization
  2. Matrix inversion - Solving complex business systems efficiently
  3. Composite and inverse functions - Understanding indirect relationships and growth models
  4. R programming for univariable functions - Practical tools for economic analysis
  5. Multivariable function visualization - Advanced decision-making with multiple factors
  6. Quadratic forms and positive definite matrices - Risk analysis and optimization theory

Preparation for Section 3: These tools provide the foundation for understanding derivatives, which measure rates of change and enable optimization in business contexts.

Key Takeaways: - Mathematics provides powerful tools for business decision-making - Multiple representations (algebraic, graphical, computational) enhance understanding - R programming enables sophisticated analysis of real business problems - Interactive learning reinforces theoretical concepts with practical applications


Total estimated lecture time: 90 minutes Interactive elements: 6 quizzes + 6 R environments Assessment: 30 comprehensive questions across 5 levels of understanding